<?php
// 激活转接页面
if (@$_REQUEST["dataArr"]) {
  // 获得需要上传的数据
  $dataArr = json_decode($_REQUEST["dataArr"], true);
  // 判断是什么类型公共交通
  if ($dataArr["what"] == "Tram") {
    echo file_get_contents($dataArr["url"]);
  } else if ($dataArr["what"] == "Bus") {
    echo file_get_contents($dataArr["url"]);
  } else if ($dataArr["what"] == "GetOpenid") {
    $op = $dataArr["op"];
    $url = "https://api.weixin.qq.com/sns/jscode2session?appid=wxa691a2d58fe8defa&secret=a5c80ec362ddea135610e4e8891dd03b&js_code=$op&grant_type=authorization_code";
    $get = file_get_contents($url);
    $canGet = json_decode($get);
    echo json_encode($canGet->openid);
  } else if ($dataArr["what"] == "Remind") {
    set_time_limit(0);
    $sleep_time = 20;
    $goNum = 0;
    while (1) {
      $date = strtotime(date("Y-m-d h:i:s"));
      $get = file_get_contents($dataArr["urlCheck"] . $date);
      $canGet = json_decode($get, true);
      if ($canGet["data"] == null) {
        continue;
      }
      foreach ($canGet["data"] as $key => $value) {
        if ($value["CurrentStation"] == $dataArr["lastStname"] && $value["LastPosition"] == 8) {
          send($dataArr);
          break 2;
        }
      }
      $goNum = $goNum + 1;
      if ($goNum == 24) {
        $dataArr["keyword4"] = "公交长时间未到站提醒取消";
        send($dataArr);
        break 1;
      }
      sleep($sleep_time);
    }
  } else {
    echo "Hello1";
  }
} else {
  echo "Hello2";
};

function send($dataArr)
{
  $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wxa691a2d58fe8defa&secret=a5c80ec362ddea135610e4e8891dd03b";
  $get = file_get_contents($url);
  $canGet = json_decode($get);
  $url = $dataArr["url"] . $canGet->access_token;
  $post_data = array(
    "touser" => $dataArr["touser"],
    "template_id" => "FDHcnAK6Eop0MMg0j_rBlUXrrSBmIEJtdjm0jNfIp1A",
    "form_id" => $dataArr["form_id"],
    "data" => array(
      "keyword1" => array(
        "value" => $dataArr["keyword1"],
        "color" => "#5a87e2"
      ),
      'keyword2' => array("value" => $dataArr["keyword2"]),
      'keyword3' => array("value" => $dataArr["keyword3"]),
      'keyword4' => array("value" => $dataArr["keyword4"]),
    ),
    "emphasis_keyword" => "keyword1.DATA",
  );
  $data = json_encode($post_data, true);
  $options = array(
    'http' => array(
      'method' => 'POST',
      'header' => 'Content-type:application/json',
      'content' => $data,
      'timeout' => 60
    )
  );
  $context = stream_context_create($options);
  echo file_get_contents($url, false, $context);
}
?>